package com.ytq.share.service.impl;

import com.ytq.share.entity.User;
import com.ytq.share.mapper.UserMapper;
import com.ytq.share.service.CustomUserService;
import com.ytq.share.service.UserService;
import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Random;

/**
 * @author yuantongqin
 * 2022/4/1
 */
@Service
public class CustomUserServiceImpl implements CustomUserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserService userService;

    @Override
    @ShardingSphereTransactionType(TransactionType.XA)
    @Transactional
    public String distributedTranUser() {

        User user = createUser(10);
        User user1 = createUser(11);
        userService.saveUser(user);
        userService.saveUser1(user1);
        return "ok";
    }

    public User createUser(long j){
        User userPO = new User();
//        int j = new Random().nextInt(100);
        userPO.setUserId(j);
        userPO.setUserName("zhang"+j);

        int y = new Random().nextInt(2);
        LocalDate now = LocalDate.now();
        int year = now.plusYears(y).getYear();
        userPO.setYear_num((long) year);
        return userPO;
    }


}
